﻿using In66.Net.Share.Controller;
using In66.Net.Share.Models.Config;
using Microsoft.Extensions.Options;
using System.Net;
using Test.Api.Models;

namespace Test.Api.Controllers.V1
{
    /// <summary>
    /// 主控制器
    /// </summary>
    [ApiVersion("1.0")]
    [AllowAnonymous]
    [Route("test/[controller]/[action]")]
    [Microsoft.AspNetCore.Authorization.Authorize(AuthenticationSchemes = BasicDefaults.AuthenticationScheme)]
    public class HomeController : BaseController
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IOptionsSnapshot<CorsConfig> _option;
        private readonly IOptionsSnapshot<TestConfig> _toption;
        private readonly IRedisProvider _redis;
        private readonly IDistributedLocker _locker;
        private readonly ICacheProvider _cache;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="redis"></param>
        /// <param name="locker"></param>
        /// <param name="cache"></param>
        /// <param name="option"></param>
        /// <param name="toption"></param>
        /// <param name="eventPublisher"></param>
        /// <param name="mongoRepository"></param>
        /// <param name="eventBus"></param>
        public HomeController(
            ILogger<HomeController> logger,
            IRedisProvider redis,
            IDistributedLocker locker,
            ICacheProvider cache,
            IOptionsSnapshot<CorsConfig> option,
            IOptionsSnapshot<TestConfig> toption
            )
        {
            _logger = logger;
            _option = option;
            _toption = toption;
            _redis = redis;
            _locker = locker;
            _cache = cache;
        }

        /// <summary>
        /// 登入
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public string BasicToken(string userName)
        {
            var token = BasicTokenValidator.PackToBase64(userName);
            return token;
        }

        /// <summary>
        /// 匿名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WeatherForecast))]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public IActionResult AllowAnonymous()
        {
            return Ok(new WeatherForecast());
        }

        /// <summary>
        /// Limit
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WeatherForecast))]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public IActionResult Limit()
        {

            var dsf = HttpContext.Request;

           var ss = Request.Headers;

            return Ok(new WeatherForecast());
        }

        /// <summary>
        /// 500错误
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(WeatherForecast))]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]

        public IActionResult Error()
        {
            return StatusCode((int)HttpStatusCode.InternalServerError);
        }

        /// <summary>
        /// 日志
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Log()
        {
            // 内置日志
            _logger.LogTrace(1000, "log Trace msg");
            _logger.LogDebug(1001, "log Debug msg");
            _logger.LogInformation(1002, "log Information msg");
            _logger.LogWarning(1003, "log Warning msg");
            _logger.LogError(1004, "log Error msg");
            _logger.LogCritical(1005, "log Critical msg");

            return "Test";
        }

        /// <summary>
        /// 配置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Config()
        {
            var res = _option.Value.IPs;

            var sss = _toption.Value.TestStr;

            return Ok(res + "====" + sss);
        }

        /// <summary>
        /// 枚举
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(EnumModel))]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public IActionResult Enum([FromBody] EnumModel enumModel)
        {
            return Ok(new EnumModel());
        }

        /// <summary>
        /// Redis基本
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RedisBasic()
        {
            _redis.StringSet("Test", "Test");

            return Ok();
        }

        /// <summary>
        /// Redis缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RedisCache()
        {
            _cache.Set("mytest", new { Id = "test", Name = "test" }, TimeSpan.FromMinutes(1));

            return Ok();
        }
    }
}
